#include "skiplist.h"
#include <gtest/gtest.h>

class SkipListTest : public ::testing::Test {
protected:
    SkipList<int, std::string> skiplist;

    void SetUp() override {
        // 初始插入几个节点
        skiplist.insert(3, "three");
        skiplist.insert(1, "one");
        skiplist.insert(5, "five");
    }

};

// 测试插入后是否能查找成功
TEST_F(SkipListTest, SearchInsertedValues) {
    std::string value;
    EXPECT_TRUE(skiplist.search(1, value));
    EXPECT_EQ(value, "one");

    EXPECT_TRUE(skiplist.search(3, value));
    EXPECT_EQ(value, "three");

    EXPECT_TRUE(skiplist.search(5, value));
    EXPECT_EQ(value, "five");
}

// 测试查找不存在的键
TEST_F(SkipListTest, SearchNonexistentKey) {
    std::string value;
    EXPECT_FALSE(skiplist.search(999, value));
}

// 测试覆盖已有键
TEST_F(SkipListTest, InsertOverwrite) {
    skiplist.insert(3, "THREE");
    std::string value;
    EXPECT_TRUE(skiplist.search(3, value));
    EXPECT_EQ(value, "THREE");
}

// 测试删除已存在的键
TEST_F(SkipListTest, RemoveKey) {
    EXPECT_TRUE(skiplist.remove(1));
    std::string value;
    EXPECT_FALSE(skiplist.search(1, value));
}

// 测试删除不存在的键
TEST_F(SkipListTest, RemoveNonexistentKey) {
    EXPECT_FALSE(skiplist.remove(42));
}
